package com.ruoyi.evaluate.infoprojectevaluate.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.evaluate.infoprojectevaluate.domain.InfoProjectEvaluate;
import com.ruoyi.evaluate.infoprojectevaluate.mapper.InfoProjectEvaluateMapper;
import com.ruoyi.evaluate.infoprojectevaluate.service.IInfoProjectEvaluateService;
import com.ruoyi.evaluate.infoprojectevaluate.vo.ProjectEvaluateDetailVo;
import com.ruoyi.evaluate.infoprojectevaluate.vo.ProjectEvaluateExamineVo;
import com.ruoyi.evaluate.infoprojectevaluateindex.domain.InfoProjectEvaluateIndex;
import com.ruoyi.evaluate.infoprojectevaluateindex.mapper.InfoProjectEvaluateIndexMapper;
import com.ruoyi.monitor.infoprojectmonitorfunds.domain.InfoProjectMonitorFunds;
import com.ruoyi.monitor.infoprojectmonitorindex.domain.InfoProjectMonitorIndex;
import com.ruoyi.monitor.infoprojectmonitorindex.mapper.InfoProjectMonitorIndexMapper;
import com.ruoyi.monitor.infoprojectmonitorrelation.domain.InfoProjectMonitorRelation;
import com.ruoyi.monitor.infoprojectmonitorrelation.mapper.InfoProjectMonitorRelationMapper;
import com.ruoyi.project.infoprojecttarget.domain.InfoProjectTarget;
import com.ruoyi.project.infoprojecttarget.mapper.InfoProjectTargetMapper;
import com.ruoyi.project.infoprojecttargetindex.domain.InfoProjectTargetIndex;
import com.ruoyi.project.infoprojecttargetindex.mapper.InfoProjectTargetIndexMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 项目评价Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-21
 */
@Service
public class InfoProjectEvaluateServiceImpl implements IInfoProjectEvaluateService {
    @Autowired
    private InfoProjectEvaluateMapper infoProjectEvaluateMapper;

    @Autowired
    private InfoProjectEvaluateIndexMapper infoProjectEvaluateIndexMapper;
    @Autowired
    private InfoProjectMonitorRelationMapper infoProjectMonitorRelationMapper;
    @Autowired
    private InfoProjectMonitorIndexMapper infoProjectMonitorIndexMapper;
    @Autowired
    private InfoProjectTargetMapper infoProjectTargetMapper;

    @Autowired
    private InfoProjectTargetIndexMapper infoProjectTargetIndexMapper;

    /**
     * 查询项目评价
     *
     * @param id 项目评价主键
     * @return 项目评价
     */
    @Override
    public InfoProjectEvaluate selectInfoProjectEvaluateById(Long id) {
        return infoProjectEvaluateMapper.selectInfoProjectEvaluateById(id);
    }

    /**
     * 查询项目评价列表
     *
     * @param infoProjectEvaluate 项目评价
     * @return 项目评价
     */
    @Override
    public List<InfoProjectEvaluate> selectInfoProjectEvaluateList(InfoProjectEvaluate infoProjectEvaluate) {
        return infoProjectEvaluateMapper.selectInfoProjectEvaluateList(infoProjectEvaluate);
    }

    /**
     * 新增项目评价
     *
     * @param infoProjectEvaluate 项目评价
     * @return 结果
     */
    @Override
    public int insertInfoProjectEvaluate(InfoProjectEvaluate infoProjectEvaluate) {
        infoProjectEvaluate.setCreateTime(DateUtils.getNowDate());
        return infoProjectEvaluateMapper.insertInfoProjectEvaluate(infoProjectEvaluate);
    }

    /**
     * 修改项目评价
     *
     * @param infoProjectEvaluate 项目评价
     * @return 结果
     */
    @Override
    public int updateInfoProjectEvaluate(InfoProjectEvaluate infoProjectEvaluate) {
        infoProjectEvaluate.setUpdateTime(DateUtils.getNowDate());
        return infoProjectEvaluateMapper.updateInfoProjectEvaluate(infoProjectEvaluate);
    }

    /**
     * 批量删除项目评价
     *
     * @param ids 需要删除的项目评价主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectEvaluateByIds(Long[] ids) {
        return infoProjectEvaluateMapper.deleteInfoProjectEvaluateByIds(ids);
    }

    /**
     * 删除项目评价信息
     *
     * @param id 项目评价主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectEvaluateById(Long id) {
        return infoProjectEvaluateMapper.deleteInfoProjectEvaluateById(id);
    }

    @Transactional
    @Override
    public int editExamineEvaluate(ProjectEvaluateExamineVo projectEvaluateExamineVo) {
        // 判断下达然后进行操作 1为已下达待填报
        InfoProjectEvaluate infoProjectEvaluate = new InfoProjectEvaluate();
            if (projectEvaluateExamineVo.getExamineStatus() == 1 ) {
                //获取关联表中的所有信息
                InfoProjectMonitorRelation infoProjectMonitorRelation = infoProjectMonitorRelationMapper.selectInfoProjectMonitorRelationByIds(projectEvaluateExamineVo.getMonitorId());

                infoProjectEvaluate=infoProjectEvaluateMapper.selectInfoProjectEvaluateById(projectEvaluateExamineVo.getEvaluateId());

                if(infoProjectEvaluate.getIsOnce()!=1) {
                    //不存在项目监控周期
                    if (infoProjectMonitorRelation != null ) {
                        if (!"".equals(infoProjectMonitorRelation.getCapitalRate()) && infoProjectMonitorRelation.getMonitorStatus() == 3) {
                            Long id = infoProjectMonitorRelation.getId();
                            List<InfoProjectMonitorIndex> infoProjectMonitorIndex = infoProjectMonitorIndexMapper.selectInfoProjectMonitorIndexByIds(id);
                            InfoProjectEvaluateIndex infoProjectEvaluateIndex = new InfoProjectEvaluateIndex();
                            for (int i = 0; i < infoProjectMonitorIndex.size(); i++) {
                                InfoProjectMonitorIndex infoProjectMonitorIndex1 = infoProjectMonitorIndex.get(i);
                                String targetOne = infoProjectMonitorIndex1.getTargetOne();
                                String targetTw = infoProjectMonitorIndex1.getTargetTwo();
                                String targetThree = infoProjectMonitorIndex1.getTargetThree();
                                String targetValue = infoProjectMonitorIndex1.getTargetValue();
                                String finishValue = infoProjectMonitorIndex1.getFinishValue();
                                Integer finishStatus = infoProjectMonitorIndex1.getFinishStatus();
                                String deviateReason = infoProjectMonitorIndex1.getDeviateReason();
                                String targetScore = infoProjectMonitorIndex1.getTargetScore();
                                infoProjectEvaluateIndex.setTargetScore(targetScore);
                                infoProjectEvaluateIndex.setTargetOne(targetOne);
                                infoProjectEvaluateIndex.setTargetTwo(targetTw);
                                infoProjectEvaluateIndex.setTargetThree(targetThree);
                                infoProjectEvaluateIndex.setTargetValue(targetValue);
                                infoProjectEvaluateIndex.setFinishValue(finishValue);
                                infoProjectEvaluateIndex.setFinishStatus(finishStatus);
                                infoProjectEvaluateIndex.setDeviateReason(deviateReason);
                                Long evaluateId = projectEvaluateExamineVo.getEvaluateId();
                                infoProjectEvaluateIndex.setProjectEvaluateId(evaluateId);
                                infoProjectEvaluateIndexMapper.insertInfoProjectEvaluateIndex(infoProjectEvaluateIndex);
                            }
                        }
                    }
                    if (infoProjectEvaluateIndexMapper.selectInfoProjectEvaluateIndexByIds(infoProjectEvaluate.getId()).isEmpty()){
                        List<InfoProjectTargetIndex> infoProjectTargetIndexList = infoProjectTargetIndexMapper.selectInfoProjectTargetIndexByProjectId(infoProjectEvaluate.getTargetId());
                        for (InfoProjectTargetIndex infoProjectTargetIndex : infoProjectTargetIndexList) {
                            InfoProjectEvaluateIndex infoProjectEvaluateIndex = new InfoProjectEvaluateIndex();
                            BeanUtils.copyProperties(infoProjectTargetIndex,infoProjectEvaluateIndex);
                            infoProjectEvaluateIndex.setTargetThree(infoProjectTargetIndex.getTargetName());
                            infoProjectEvaluateIndex.setProjectEvaluateId(projectEvaluateExamineVo.getEvaluateId());
                            infoProjectEvaluateIndexMapper.insertInfoProjectEvaluateIndex(infoProjectEvaluateIndex);
                        }
                    }

                }
            }
            infoProjectEvaluate.setId(projectEvaluateExamineVo.getEvaluateId());


            infoProjectEvaluate.setIsOnce(1);
            //下达的状态
            infoProjectEvaluate.setExamineStatus(projectEvaluateExamineVo.getExamineStatus());

            infoProjectEvaluate.setExamineRemarks(projectEvaluateExamineVo.getExamineRemarks());
            //更新资金监控情况  获取该项目的资金最后一条数据
            /*InfoProjectMonitorFunds infoProjectMonitorFunds = infoProjectMonitorRelationMapper.selectLaseMonitorTimeFunds(projectEvaluateExamineVo.getMonitorId());
            if (null != infoProjectMonitorFunds) {
                infoProjectEvaluate.setTotalFundsRate(infoProjectMonitorFunds.getTotalFundsExecuteRate());
                infoProjectEvaluate.setCentralFundRate(infoProjectMonitorFunds.getCentralFundExecuteRate());
                infoProjectEvaluate.setProvincialFundsRate(infoProjectMonitorFunds.getProvincialFundsExecuteRate());
                infoProjectEvaluate.setOtherFundRate(infoProjectMonitorFunds.getOtherFundExecuteRate());

                infoProjectEvaluate.setTotalFundsExecute(infoProjectMonitorFunds.getTotalFundsExecute());
                infoProjectEvaluate.setCentralFundExecute(infoProjectMonitorFunds.getCentralFundExecute());
                infoProjectEvaluate.setProvincialFundsExecute(infoProjectMonitorFunds.getProvincialFundsExecute());
                infoProjectEvaluate.setTotalFundsExecute(infoProjectMonitorFunds.getTotalFundsExecute());
                infoProjectEvaluate.setOtherFundExecute(infoProjectMonitorFunds.getOtherFundExecute());

                //得分  = 分值 * 执行率  分值默认都是10分
                if (infoProjectMonitorFunds.getTotalFundsExecuteRate() != null) {
                    infoProjectEvaluate.setTotalFundsScore(infoProjectMonitorFunds.getTotalFundsExecuteRate().multiply(new BigDecimal(10)));
                }
                if (infoProjectMonitorFunds.getCentralFundExecuteRate() != null) {
                    infoProjectEvaluate.setCentralFundScore(infoProjectMonitorFunds.getCentralFundExecuteRate().multiply(new BigDecimal(10)));
                }
                if (infoProjectMonitorFunds.getProvincialFundsExecuteRate() != null) {
                    infoProjectEvaluate.setProvincialFundsScore(infoProjectMonitorFunds.getProvincialFundsExecuteRate().multiply(new BigDecimal(10)));
                }
            }*/
        //修改数据
        int i = infoProjectEvaluateMapper.updateInfoProjectEvaluate(infoProjectEvaluate);
        return i;
    }



    @Override
    public ProjectEvaluateDetailVo getProjectEvaluateDetail(Long id) {
        ProjectEvaluateDetailVo detailVo = new ProjectEvaluateDetailVo();
        //1.查询项目信息
        InfoProjectEvaluate infoProjectEvaluate = infoProjectEvaluateMapper.selectInfoProjectEvaluateById(id);
        //2.查询指标信息
      /*  InfoProjectEvaluateIndex infoProjectMonitorIndex = new InfoProjectEvaluateIndex();
        infoProjectMonitorIndex.setProjectEvaluateId(id);
        List<InfoProjectEvaluateIndex> infoProjectEvaluateIndices = infoProjectEvaluateIndexMapper.selectInfoProjectEvaluateIndexList(infoProjectMonitorIndex);*/
        ;
        detailVo.setInfoProjectEvaluate(infoProjectEvaluate);
        detailVo.setInfoProjectEvaluateIndexList(infoProjectEvaluateIndexMapper.selectEvaluateIndexList(id));

        return detailVo;
    }

    @Override
    public List<InfoProjectEvaluate> selectInfoProjectEvaluateBudgetUnitList(InfoProjectEvaluate infoProjectEvaluate) {
        return infoProjectEvaluateMapper.selectInfoProjectEvaluateBudgetUnitList(infoProjectEvaluate);
    }

    @Override
    public int uploadFiles(InfoProjectEvaluate infoProjectEvaluate) {
        Long id=infoProjectEvaluate.getId();
        //读取这个文件地址
        String fileUrl = infoProjectEvaluate.getReportUrl();
        String fileName=infoProjectEvaluate.getReportName();
        InfoProjectEvaluate infoProjectEvaluate1=infoProjectEvaluateMapper.selectInfoProjectEvaluateById(id);
        infoProjectEvaluate1.setReportUrl(fileUrl);
        infoProjectEvaluate1.setReportName(fileName);
        int i=infoProjectEvaluateMapper.updateInfoProjectEvaluate(infoProjectEvaluate1);
        return i;
    }

    @Override
    public List<InfoProjectEvaluate> selectInfoProjectEvaluateByIdss(Long id) {
        return infoProjectEvaluateMapper.selectInfoProjectEvaluateByIdss(id);
    }
}
